package org.bits.ui;

import java.awt.*;

import javax.swing.*;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLEditorKit;

import org.bits.common.IconConstants;
import org.bits.data.Finding;
import org.bits.io.FindingHtmlSerializer;

/*******************************************************************************
 * 
 ******************************************************************************/
public class ResultsView
{
    /**  */
    private JPanel panel;
    /**  */
    private JEditorPane itemField;

    /***************************************************************************
     * 
     **************************************************************************/
    public ResultsView()
    {
        panel = new JPanel( new GridBagLayout() );
        itemField = createEditorPane();
        JScrollPane itemScrollPane = new JScrollPane( itemField );

        GridBagConstraints constraints;

        constraints = new GridBagConstraints( 0, 0, 1, 1, 1.0, 0.0,
            GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
            new Insets( 0, 0, 0, 0 ), 0, 0 );
        panel.add( createToolbar(), constraints );

        constraints = new GridBagConstraints( 0, 1, 1, 1, 1.0, 1.0,
            GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets( 0,
                0, 0, 0 ), 0, 0 );
        panel.add( itemScrollPane, constraints );
    }

    /***************************************************************************
     * @return
     **************************************************************************/
    private JToolBar createToolbar()
    {
        JToolBar toolbar = new JToolBar();
        JButton button;

        toolbar.setBorderPainted( false );
        toolbar.setFloatable( false );
        toolbar.setRollover( true );

        button = new JButton(
            IconConstants.loader.getIcon( IconConstants.SAVE_16 ) );
        button.setToolTipText( "Save" );
        toolbar.add( button );

        button = new JButton(
            IconConstants.loader.getIcon( IconConstants.EDIT_16 ) );
        button.setToolTipText( "Edit" );
        toolbar.add( button );

        return toolbar;
    }

    /***************************************************************************
     * @return
     **************************************************************************/
    private JEditorPane createEditorPane()
    {
        JEditorPane editorPane = new JEditorPane(
            new HTMLEditorKit().getContentType(), "" );
        Font font = UIManager.getFont( "Label.font" );
        font = font.deriveFont( 14.0f );
        HTMLDocument doc = ( HTMLDocument )editorPane.getDocument();
        String bodyRule = "body { font-family: " + font.getFamily() + "; " +
            "font-size: " + font.getSize() + "pt;" + " margin-top:0px; " +
            "margin-bottom:10px; " + "margin-right:10px; " +
            "margin-left:10px; }";
        doc.getStyleSheet().addRule( bodyRule );

        editorPane.setBorder( null );
        editorPane.setEditable( false );

        return editorPane;
    }

    /***************************************************************************
     * @return
     **************************************************************************/
    public Component getView()
    {
        return panel;
    }

    /***************************************************************************
     * @param finding
     **************************************************************************/
    public void setFinding( Finding finding )
    {
        FindingHtmlSerializer findingSerializer = new FindingHtmlSerializer();

        itemField.setText( findingSerializer.serialize( finding ) );
    }

    /***************************************************************************
     * 
     **************************************************************************/
    public void clear()
    {
        itemField.setText( "" );
    }
}
